import {createRouter, createWebHistory} from 'vue-router';
import FormDesign from '@/views/FormDesign';
import FormFlowDesign from '@/views/FormFlowDesign';
import Dashboard from '@/views/dashboard';
import PlatApp from '@/views/plat/app';
import AppEditor from '@/views/plat/app/AppEditor';
import Login from '@/views/Login';
import SingleLayout from '@/components/layout/SingleLayout.vue';
import store from '@/store';
import {isEmpty, isNotEmpty} from '@/utils/CommonUtils';
import FormApproveComponent from '@/components/platwork/FormApproveComponent';
import StartProcess from '@/components/platwork/StartProcess';
import FormDetailView from '@/views/platwork/FormDetailView';
import CommonFormEditor from '@/components/platwork/CommonFormEditor'

const whiteList = ['/login'];

const routes = [
  {
    name: 'login',
    path: '/login',
    component: Login
  },

  {
    id: 'home',
    path: '/',
    name: 'home',
    component: SingleLayout,
    children: [
      {
        path: '/',
        name: 'home',
        component: Dashboard,
        meta: {
          title: '首页'
        },
      },
      {
        path: '/form/add',
        name: 'formAdd',
        component: FormDesign,
      },
      {
        path: '/form/edit',
        name: 'formEdit',
        component: FormDesign,
      },
      {
        path: '/form/flow/edit',
        name: 'formFlowAdd',
        component: FormFlowDesign,
      },
      {
        path: '/plat/app',
        component: PlatApp,
        meta: {
          title: '应用定制中心'
        },
      },
      {
        name: 'appEdit',
        path: '/plat/app/edit',
        component: AppEditor
      },
      {
        name: 'platworkDetail',
        path: '/plat/work/detail',
        component: FormDetailView,
        meta: {
          title: '详情'
        },
      },
      {
        name: 'startProcess',
        path: '/plat/work/startProcess',
        component: StartProcess
      },
      {
        name: 'DefaultApproveComponent',
        path: '/plat/work/approve',
        component: FormApproveComponent
      },
      {
        name: 'CommonFormEditor',
        path: '/plat/work/formEdit',
        component: CommonFormEditor
      }]
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});

router.beforeEach((to, from, next) => {
  let userInfo = store.getters['user/userInfo'];
  if ((isEmpty(userInfo) || isEmpty(userInfo.token)) && to.path !== '/login') {
    next('/login');
    return;
  }
  next();
});

router.afterEach((to, from) => {
  if (isNotEmpty(to) && isNotEmpty(to.meta) && isNotEmpty(to.meta.title)) {
    document.title = to.meta.title;
  }
});

export default router;
